
import UIKit
import SVGAPlayer
import SnapKit
class XWLoadingViewController: UIViewController, SVGAPlayerDelegate {
    @IBOutlet weak var XWSVGBackView: UIView!
    private var XWSvgaPlayer: SVGAPlayer!
    @IBOutlet weak var XWLoadingLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        showUI()
        
    }
}
extension XWLoadingViewController {
    private func showUI() {
        XWSvgaPlayer = SVGAPlayer.init()
        XWSVGBackView.addSubview(XWSvgaPlayer)
        XWSvgaPlayer.snp.makeConstraints { make in
            make.top.equalTo(XWSVGBackView)
            make.left.equalTo(XWSVGBackView)
            make.right.equalTo(XWSVGBackView)
            make.bottom.equalTo(XWSVGBackView)
        }
        XWSvgaPlayer.loops = 1
        XWSvgaPlayer.delegate = self
        XWSvgaPlayer.clearsAfterStop = true
        let parser = SVGAParser.init()
        parser.parse(withNamed: "loading15s", in: nil) { [self] videoItem in
            XWSvgaPlayer.videoItem = videoItem
            XWSvgaPlayer.startAnimation()
        }
    }
    func svgaPlayer(_ player: SVGAPlayer!, didAnimatedToPercentage percentage: CGFloat) {
        let tempPercentage = String.init(format: "%.f", percentage*100)
        XWLoadingLabel.text = "Loading...(" + (tempPercentage) + "%)"
        if(percentage == 1.0){
            let vc = XWHomeViewController.init()
            PresentViewController(vc)
        }
    }
}
